package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.vo.LoginUserVO;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginUserVO login(LoginDto dto) {
        //参数异常抛出异常
        if(dto == null){
            log.error("参数异常");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName,dto.getLoginName());
        User user = userMapper.selectOne(wrapper);
        LoginUserVO loginUserVo = new LoginUserVO();
        if(user == null){ //代表用户不存在得
            loginUserVo.setMsg("用户不存在得");
            return loginUserVo;
        }
        if(!user.getPassword().equals(dto.getPassword())){ //代表密码错误
            loginUserVo.setMsg("代表密码错误");
            return loginUserVo;
        }
        //密码相等

        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }
}
